<template>

    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="props.view_address.id > 0"
        @close="baTable.toggleForm"
        width="80%"
        :before-close="close"
    >

        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                查看地址
            </div>
        </template>

    <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">

        <div class="ba-table-box">
            

                <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

                <!-- 表格顶部菜单 -->
                <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
                <TableHeader :buttons="['add', 'delete', 'quickSearch', 'columnDisplay']" quick-search-placeholder="搜索 商品名称">
                </TableHeader>

                <!-- 表格 -->
                <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
                <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
                <Table ref="tableRef">
                    <template #url>
                        <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                        <el-table-column label="链接地址" align="center" width="150" class-name="shipment-status">
                            <template #default="scope">
                                
                                <div style="margin-top: 5px;">
                                    <span class="btn" v-blur @click="viewAddress(scope.row.url)">查看地址</span>
                                </div>
                            </template>
                        </el-table-column>
                    </template>
                </Table>

                <!-- 表单 -->
                <AddGoodsAddress />
            
            

        </div>
    </el-scrollbar>

    </el-dialog>
    
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import AddGoodsAddress from './addGoodsAddress.vue'
import PlaceOrderForm from './placeOrderForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'


const emit = defineEmits(["closeViewAddress"])

defineOptions({
    name: 'daily/goods/viewAddress',
})

const { t } = useI18n()
const tableRef = ref()


interface Props {
    view_address: anyObj
}

const props = defineProps<Props>()

let optButtons = defaultOptButtons([])


// 关闭选择样品的弹窗
const close = (done: () => void) => {
    emit("closeViewAddress")
    done();
}

const viewAddress = (url) => {
    window.open(url);
}



// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'download', // 按钮名称
        title: 'download', // 鼠标放置时的 title 提示
        class: 'btn',
        text: '取消收藏', // 直接在按钮内显示的文字，title 有值时可为空
        type: '', // 按钮类型，请参考 element plus 的按钮类型
        icon: '', // 按钮 icon
        disabledTip: true, // tipButton 禁用 tip
        click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
            console.log('cancel collection')
            cancelCollectionApi(row.id).then((e) => {
                console.log(e)
                if(e.code == 1){
                    row.collection = null;
                }
            })
        },
        display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
            return row.collection == null ? false : true;
        },
        disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
            // return row.file ? false : true;
        },
        attr: {} // 自定义el-button属性
    },
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'edit',
        title: 'edit',
        text: '编辑',
        type: 'primary',
        class: 'btn btn-primary',
        icon: '',
    },{
        render: 'confirmButton',
        name: 'delete',
        title: '',
        text: '删除',
        type: 'danger',
        class: 'btn btn-danger',
        icon: '',
        popconfirm: {
            confirmButtonText: t('Delete'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'danger',
            title: t('Are you sure to delete the selected record?'),
        }
    }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)

// const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])




const showGoods = (type) => {
    state.show_goods = type;
    baTable.table.filter!.show_goods = type;
    baTable.getIndex();
}


const showImportFromFun = () => {
    state.showImportFrom = true;
    return false;
}

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/system/daily.GoodsAddress/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('购买平台'), prop: 'platform', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('单价'), prop: 'price', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('单位'), prop: 'unit', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { 
                label: t('链接地址'), 
                render: "slot", 
                prop: 'url', 
                align: 'center',
                slotName: 'url'
            },
            { label: t('备注'), prop: 'note', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            
            { label: t('Operate'), align: 'center', render: 'buttons', buttons: optButtons, operator: false, width: 220 },
        ],
        dblClickNotEditColumn: ['all'],
        defaultOrder: { prop: 'id', order: 'desc' },
    },
    {
        defaultItems: { goods_id: props.view_address.id, name: null, color: null, size: null, stock: null, stock_total: null, cover: null, price: null, supply_discount: 0, supply_price: 0, cycle_15: null, cycle_30: null, upstream_link: null },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.table.filter!.id = props.view_address.id;
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">
:deep(.el-image){
    width: 50px;
    height: unset;
}
:deep(.ba-data-table .btn){
    padding: 4px 14px;
}


:deep(.ba-data-table .margin-left-none:last-child){
    margin-bottom: 0px;
}
:deep(.table-operate-text){
    padding-left: 0;
}
:deep(.font-size-13){
    font-size: 13px;
}
</style>
